﻿<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="text"/>
  <xsl:variable name="GROUP_AND">And</xsl:variable>
  <xsl:variable name="GROUP_OR">Or</xsl:variable>
  <xsl:variable name="COLUMN_ID">Id</xsl:variable>
  <xsl:variable name="COLUMN_TITLE">Title</xsl:variable>
  <xsl:variable name="COLUMN_TREEID">TreeId</xsl:variable>
  <xsl:variable name="OP_EQUALS">equals</xsl:variable>
  <xsl:variable name="OP_NOTEQUALS">notEquals</xsl:variable>
  <xsl:variable name="OP_CONTAINS">contains</xsl:variable>
  <xsl:variable name="OP_NOTCONTAINS">notContains</xsl:variable>
  <xsl:variable name="OP_LESS">less</xsl:variable>
  <xsl:variable name="OP_GREATER">greater</xsl:variable>
  <xsl:variable name="OP_EQUALSLESS">equalsLess</xsl:variable>
  <xsl:variable name="OP_EQUALSGREATER">equalsGreater</xsl:variable>
  <xsl:variable name="OP_UNDER">under</xsl:variable>
  <xsl:variable name="OP_NOTUNDER">notUnder</xsl:variable>

  <xsl:template match="/Query">
    <xsl:apply-templates select="Group"/>
  </xsl:template>

  <xsl:template match="Group">
    <xsl:value-of select="'('"/>
    <xsl:for-each select="./*">
      <xsl:apply-templates select="."/>
      <xsl:if test="count(following-sibling::*)>0">
        <xsl:choose>
          <xsl:when test="parent::*/@GroupOperator=$GROUP_AND">
            <xsl:value-of select="' AND '"/>
          </xsl:when>
          <xsl:when test="parent::*/@GroupOperator=$GROUP_OR">
            <xsl:value-of select="' OR '"/>
          </xsl:when>
        </xsl:choose>
      </xsl:if>
    </xsl:for-each>
    <xsl:value-of select="')'"/>
  </xsl:template>

  <xsl:template match="Expression">
    <xsl:choose>
      <xsl:when test="@Column=$COLUMN_ID">
        <xsl:choose>
          <xsl:when test="@Operator=$OP_EQUALS">
            <xsl:value-of select="concat('ID=',.)"/>
          </xsl:when>
          <xsl:when test="@Operator=$OP_NOTEQUALS">
            <xsl:value-of select="concat('ID&lt;&gt;', .)"/>
          </xsl:when>
          <xsl:when test="@Operator=$OP_LESS">
            <xsl:value-of select="concat('ID&lt;', .)"/>
          </xsl:when>
          <xsl:when test="@Operator=$OP_GREATER">
            <xsl:value-of select="concat('ID&gt;', .)"/>
          </xsl:when>
          <xsl:when test="@Operator=$OP_EQUALSLESS">
            <xsl:value-of select="concat('ID&lt;=', .)"/>
          </xsl:when>
          <xsl:when test="@Operator=$OP_EQUALSGREATER">
            <xsl:value-of select="concat('ID&gt;=', .)"/>
          </xsl:when>
        </xsl:choose>
      </xsl:when>
      <xsl:when test="@Column=$COLUMN_TITLE">
        <xsl:choose>
          <xsl:when test="@Operator=$OP_EQUALS">
            <xsl:text>Title=&apos;</xsl:text>
            <xsl:value-of select="."/>
            <xsl:text>&apos;</xsl:text>
          </xsl:when>
          <xsl:when test="@Operator=$OP_NOTEQUALS">
            <xsl:text>Title&lt;&gt;'</xsl:text>
          <xsl:text>&apos;</xsl:text>
            <xsl:value-of select="."/>
          </xsl:when>
          <xsl:when test="@Operator=$OP_CONTAINS">Title LIKE '%<xsl:value-of select="."/>%'</xsl:when>
          <xsl:when test="@Operator=$OP_NOTCONTAINS">Title NOT LIKE ='%<xsl:value-of select="."/>%'</xsl:when>
        </xsl:choose>
      </xsl:when>
      <xsl:when test="@Column=$COLUMN_TREEID">
        <xsl:choose>
          <xsl:when test="@Operator=$OP_EQUALS">FeatureId=<xsl:value-of select="."/></xsl:when>
          <xsl:when test="@Operator=$OP_NOTEQUALS">FeatureId&lt;&gt;<xsl:value-of select="."/></xsl:when>
          <xsl:when test="@Operator=$OP_UNDER">FeatureId IN (SELECT Id FROM FeatureAncestry WHERE AncestorFeatureId=<xsl:value-of select="."/> OR AncestorFeatureId=<xsl:value-of select="."/>)</xsl:when>
          <xsl:when test="@Operator=$OP_NOTUNDER">FeatureId NOT IN (SELECT Id FROM FeatureAncestry WHERE AncestorFeatureId=<xsl:value-of select="."/> OR AncestorFeatureId=<xsl:value-of select="."/>)</xsl:when>
        </xsl:choose>
      </xsl:when>
    </xsl:choose>
  </xsl:template>
</xsl:stylesheet>
